package com.changgou.goods.controller;

import com.changgou.framework.entity.PageResult;
import com.changgou.framework.entity.Result;
import com.changgou.framework.entity.StatusCode;
import com.changgou.goods.service.TemplateService;
import com.haha.goods.entity.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 */
@RestController
@RequestMapping("/template")
@CrossOrigin
public class TemplateController {
    @Autowired
    private TemplateService templateService;
    /***
     * Template分页条件搜索实现
     * @param template
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) Template template, @PathVariable int page, @PathVariable  int size){
        //执行搜索
        PageResult pageInfo = templateService.findPage(template, page, size);
        return Result.ok(pageInfo);
    }
    /***
     * Template分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        PageResult pageInfo = templateService.findPage(null,page, size);
        return Result.ok(pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param template
     * @return
     */
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false)  Template template){
        PageResult pageResult = templateService.findPage(template,1,Integer.MAX_VALUE);
        return Result.ok(pageResult.getRows());
    }
    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        boolean b = templateService.removeById(id);
        return b?Result.ok(null):Result.error("删除失败");
    }
    /***
     * 修改Template数据
     * @param template
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Template template,@PathVariable Integer id){
        //设置主键值
        template.setId(id);
        //修改数据
        boolean b = templateService.updateById(template);
        return b?Result.ok(null):Result.error("更新失败");
    }
    /***
     * 新增Template数据
     * @param template
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Template template){
        boolean b = templateService.save(template);
        return b?Result.ok(null):Result.error("新增失败");
    }
    /***
     * 根据ID查询Template数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        //根据ID查询
        Template template = templateService.getById(id);
        return Result.ok(template);
    }

    /***
     * 查询Template全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Template> templates = templateService.list();
        return Result.ok(templates);
    }

    /***
     * 根据分类查询模板数据
     * @param id:分类ID
     */
    @GetMapping(value = "/category/{id}")
    public Result findByCategoryId(@PathVariable(value = "id")Integer id){
        //调用Service查询
        Template template = templateService.findByCategoryId(id);
        return Result.ok(template);
    }

}
